(function () {
    function ChatAlertUI() {
        ChatAlertUI.super(this);
        this.isPopup = false;
        this.custom = true;

        this._chatText = [];
        this._chatEmo = [];

        this.chatText.array = [];
        this.chatEmo.array = [];

        this.bindEvent();
    }
    Laya.class(ChatAlertUI, "ui.Alert.Chat", ui.Alert.ChatUI);

    var _proto = ChatAlertUI.prototype;

    /**
     * @privite
     * 绑定相关事件
     */
    _proto.bindEvent = function () {
        this.alertMask.on(Laya.Event.CLICK, this, function () {
            this.close();
        });
        //关联Tab与ViewStack
        this.btnChatTab.selectHandler = this.chatCon.setIndexHandler;

        //发送文字聊天命令
        this.chatText.selectHandler = new Laya.Handler(this, function (index) {
            if(index < 0){return;}

            viewer.publish("io.emit", {
                cmd : "chat::send",
                params : {
                    token : token,
                    chatId : this._chatText[index].chatId
                }
            });

            this.close();
        });

        //发送表情命令
        this.chatEmo.selectHandler = new Laya.Handler(this, function (index) {
            if(index < 0){return;}

            viewer.publish("io.emit", {
                cmd : "chat::sendExpression",
                params : {
                    token : token,
                    expressionId : this._chatEmo[index].expressionId
                }
            });

            this.close();
        });

        viewer.register("chat::list", this.updateText.bind(this));
        viewer.register("chat::expressionList", this.updateEmotion.bind(this));
    }

    /**
     * @public
     * 更新表情的List数据
     * 
     * @param {Object} res 表情数据
     */
    _proto.updateText = function (res) {
        var chatText = [];
        this._chatText = res.list;

        for(var i in res.list){
            chatText.push({
                chattext : res.list[i].content
            });
        }

        this.chatText.array = chatText;
    }
    /**
     * @public
     * 更新文字聊天的List数据
     * 
     * @param {Object} res 文字聊天数据
     */
    _proto.updateEmotion = function (res) {
        var chatEmo = [];
        this._chatEmo = res.list;

        for(var i in res.list){
            chatEmo.push({
                emotion : res.list[i].img
            });
        }

        this.chatEmo.array = chatEmo;
        //this.chatEmo.totalPage = Math.ceil(chatEmo.length / 9);
    }
    /**
     * @public
     * 显示聊天弹窗，发送获取数据的命令
     */
    _proto.display = function () {
        this.isPopup = true;
        this.width = Laya.stage.width;

        viewer.publish("io.emit", {cmd : "chat::list"});
        viewer.publish("io.emit", {cmd : "chat::expressionList"});

        this.btnChatTab.selectedIndex = 0;
        this.chatText.selectedIndex = -1;
        this.chatEmo.selectedIndex = -1;

        this.alertWrap.alpha = 0;
        this.alertWrap.scale(0, 0);

        Arena.Stage.addChild(this);
        Laya.Tween.to(this.alertWrap, {scaleX : 1, scaleY : 1, alpha : 1}, 500, Laya.Ease.bounceOut);
    }
    /**
     * @public
     * 关闭聊天弹窗，重置相关变量
     */
    _proto.close = function () {
        this.isPopup = false;

        this._chatText = [];
        this._chatEmo = [];

        this.chatText.array = [];
        this.chatEmo.array = [];

        this.removeSelf();
    }
})();